package com.gaff.emp.core.modules.auth.controller;

import com.gaff.emp.core.R;
import com.gaff.emp.core.modules.auth.LoginUserInfo;
import com.gaff.emp.core.modules.auth.annotation.Auth;
import com.gaff.emp.core.modules.auth.annotation.LoginUser;
import com.gaff.emp.core.modules.auth.form.SsoLoginForm;
import com.gaff.emp.core.modules.auth.form.SsoPasswordForm;
import com.gaff.emp.core.syslog.annotation.SysOperaLog;
import com.gaff.emp.core.modules.auth.form.LoginForm;
import com.gaff.emp.core.modules.auth.service.AuthService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private AuthService authService;

    @GetMapping("/captcha.jpg")
    public void captcha(HttpServletResponse response, String uuid) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = authService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    @SysOperaLog(value = "登录")
    public R login(@RequestBody @Valid LoginForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        return R.ok(authService.login(form));
    }

    @PostMapping("/logout")
    @ApiOperation("登出")
    @SysOperaLog(value = "登出")
    @Auth
    public R logout(@LoginUser LoginUserInfo userInfo) {
        authService.logout(userInfo);
        return R.ok();
    }

    @PostMapping("/ssoLogin")
    @ApiOperation("SSO单点登录")
    @SysOperaLog(value = "SSO单点登录", isPersistence = false)
    public R info(@RequestBody @Valid SsoLoginForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        return R.ok(authService.ssoLogin(form));
    }

    @PostMapping("/ssoPassword")
    @ApiOperation("SSO修改密码")
    @SysOperaLog(value = "SSO修改密码")
    @Auth
    public R ssoPassword(@RequestBody @Valid SsoPasswordForm form, @LoginUser LoginUserInfo userInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        authService.ssoPassword(userInfo, form);
        return R.ok();
    }

}
